<template>
  <div class="app-container">
    <transition name="el-zoom-in-top">
      <el-form
        size="small"
        :inline="true"
        v-show="showSearch"
        @keyup.enter.native="getDataList()"
      >
        <el-form-item label="患者编号">
          <el-input
            style="width: 230px"
            v-model="queryParams.patientId"
            placeholder="请输入患者编号"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="患者姓名">
          <el-input
            style="width: 230px"
            v-model="queryParams.name"
            placeholder="请输入患者姓名"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="患者来源">
          <el-cascader
            style="width: 230px"
            :props="defaultProps"
            @change="handleSubChange"
            clearable
            :options="deptOptions"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker
            v-model="dateRange"
            style="width: 230px"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['00:00:00', '23:59:59']"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="门诊号/住院号">
          <el-input
            style="width: 230px"
            v-model="queryParams.patientNumber"
            placeholder="请输入门诊号"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="getDataList"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </transition>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-data-line"
          size="mini"
          @click="goEvaluation()"
          >数据分析</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-dropdown
          size="mini"
          @command="(command) => handleAddCommand(command)"
          v-hasPermi="['gonitis:patient:info:add']"
        >
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            v-hasPermi="['gonitis:patient:info:add']"
            >新增</el-button
          >
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item
              v-for="item in newPatients"
              :key="item.id"
              :command="item.id"
              >患者姓名[ {{ item.name }} ] 创建时间[
              {{ item.createTime }} ]</el-dropdown-item
            >
          </el-dropdown-menu>
        </el-dropdown>
      </el-col>
      <el-col :span="1.5">
        <el-dropdown
          style="margin-right: 5px"
          size="mini"
          @command="(command) => handleEditCommand(command, -1)"
          v-hasPermi="['gonitis:patient:info:edit']"
        >
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            v-hasPermi="['gonitis:patient:info:edit']"
            >修改</el-button
          >
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item
              command="editBasicInfo"
              v-hasPermi="['gonitis:patient:info:edit']"
              >修改基本入组信息</el-dropdown-item
            >
            <el-dropdown-item
              command="editDiseaseInfoList"
              v-hasPermi="['gonitis:patient:info:edit']"
              >修改疾病信息</el-dropdown-item
            >
          </el-dropdown-menu>
        </el-dropdown>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="single"
          @click="deleteHandle(-1)"
          v-hasPermi="['gonitis:patient:info:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getDataList"
      ></right-toolbar>
    </el-row>
    <el-table
      :data="dataList"
      v-loading="loading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column type="selection" align="center" width="50"></el-table-column>
      <el-table-column prop="id" align="center" label="编号"></el-table-column>
      <el-table-column
        prop="patientId"
        align="center"
        label="患者编号"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column prop="name" align="center" label="患者姓名"></el-table-column>
      <el-table-column
        prop="subTitle"
        align="center"
        label="患者来源"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column prop="doctorId" align="center" label="医生编号"></el-table-column>
      <el-table-column prop="doctorName" align="center" label="医生姓名">
        <template slot-scope="scope">
          <span>{{ scope.row.doctorName ? scope.row.doctorName : "暂无" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="patientNumber"
        align="center"
        label="门诊号"
        :show-overflow-tooltip="true"
      ></el-table-column>

      <el-table-column prop="sex" header-align="center" align="center" label="性别">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex" />
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        align="center"
        width="100px"
        label="创建时间"
      ></el-table-column>
      <el-table-column
        class-name="small-padding fixed-width"
        align="center"
        width="150px"
        label="操作"
      >
        <template slot-scope="scope">
          <el-dropdown
            style="margin-right: 5px"
            size="mini"
            @command="(command) => handleEditCommand(command, scope.row)"
            v-hasPermi="['gonitis:patient:info:edit']"
          >
            <el-button
              type="text"
              size="mini"
              icon="el-icon-edit"
              v-hasPermi="['gonitis:patient:info:edit']"
              >修改</el-button
            >
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                command="editBasicInfo"
                v-hasPermi="['gonitis:patient:info:edit']"
                >修改基本入组信息</el-dropdown-item
              >
              <el-dropdown-item
                command="editDiseaseInfoList"
                v-hasPermi="['gonitis:patient:info:edit']"
                >修改疾病信息</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
          <el-button
            type="text"
            size="mini"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
            >删除</el-button
          >
          <el-dropdown
            size="mini"
            @command="(command) => handleMoreCommand(command, scope.row)"
            v-hasPermi="['gonitis:patient:info:detail', 'gonitis:patient:info:add']"
          >
            <el-button size="mini" type="text" icon="el-icon-d-arrow-right"
              >更多</el-button
            >
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                command="handleInfoDetail"
                icon="el-icon-view"
                v-hasPermi="['gonitis:patient:info:detail']"
                >信息详情</el-dropdown-item
              >
              <el-dropdown-item
                command="handleAddDisease"
                icon="el-icon-plus"
                v-hasPermi="['gonitis:patient:info:add']"
                >新增疾病信息</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getDataList"
    />
    <!-- 疾病信息列表对话框 -->
    <el-dialog
      title="选择疾病信息"
      :visible.sync="editDiseaseVisible"
      width="40%"
      :v-loading="editDiseaseLoading"
      :before-close="editDiseaseClose"
    >
      <el-table :data="editDiseaseList">
        <el-table-column
          align="center"
          width="100px"
          property="name"
          label="添加次数"
        ></el-table-column>
        <el-table-column
          align="center"
          width="100px"
          property="date"
          label="添加时间"
        ></el-table-column>
        <el-table-column align="center" width="100px" label="KOA等级">
          <template slot-scope="scope">
            <el-tag>{{ scope.row.koaGrade }}级</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          width="150px"
          :show-overflow-tooltip="true"
          property="koaDescribe"
          label="KOA描述"
        ></el-table-column>
        <el-table-column align="center" width="100px" label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="mini"
              icon="el-icon-delete"
              @click="removeDiseaseInfo(scope.row)"
              v-if="isDelete && scope.row.addCount === editDiseaseList.length"
              v-hasPermi="['gonitis:patient:info:remove']"
              >删除</el-button
            >
            <el-button
              type="text"
              size="mini"
              icon="el-icon-edit"
              @click="editDiseaseInfo(scope.row)"
              v-if="!isDelete"
              v-hasPermi="['gonitis:patient:info:edit']"
              >修改</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <info-detail v-if="detailVisible" ref="infoDetail"></info-detail>
  </div>
</template>

<script>
import InfoDetail from "./components/infoDetail";
import { subTree } from "@/api/gonitis/illSub";
import { basicInfoList, getNewPatients, removeBasicInfo } from "@/api/gonitis/basicInfo";
import { getAddCount, removeDiseaseInfo } from "@/api/gonitis/diseaseInfo";
export default {
  name: "InfoIndex",
  dicts: ["sys_user_sex"],
  data() {
    return {
      // 详情可见
      detailVisible: false,
      // 是否删除疾病信息
      isDelete: false,
      // 数据加载
      editDiseaseLoading: false,
      // 疾病信息列表数据
      editDiseaseList: [],
      // 开启疾病信息列表对话框
      editDiseaseVisible: false,
      // 病情多选框映射
      defaultProps: {
        children: "children",
        label: "label",
        value: "id",
      },
      // 没有绑定医生的用户信息
      newPatients: [],
      // 显示搜索栏
      showSearch: true,
      // 部门树选项
      deptOptions: undefined,
      // 非单个禁用
      single: true,
      // 数据集合
      dataList: [],
      // 时间条件
      dateRange: [],
      // 搜索参数
      queryParams: {
        pageSize: 10,
        pageNum: 1,
        name: undefined,
        patientNumber: undefined,
        patientId: undefined,
        subId: undefined,
      },
      total: 0,
      loading: false,
      ids: [],
    };
  },
  components: {
    InfoDetail,
  },

  created() {
    this.init();
  },

  methods: {
    /** 跳转到BMI分析页面 */
    goEvaluation() {
      this.$router.push({
        name: "Evaluation",
      });
    },
    /** 删除病情信息 */
    removeDiseaseInfo(row) {
      const diseaseId = row.id;
      this.$modal
        .confirm('是否确认删除【信息管理】编号为"' + diseaseId + '"的疾病信息？')
        .then(() => {
          this.editDiseaseLoading = true;
          return removeDiseaseInfo(diseaseId);
        })
        .then(() => {
          this.editDiseaseClose();
          this.getDataList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.editDiseaseClose();
          this.editDiseaseLoading = false;
        });
    },
    /** 删除逻辑 */
    deleteHandle(row) {
      let basicId;
      if (row === -1) {
        basicId = this.ids[0];
      } else {
        basicId = row.id;
      }
      // 先去数据库查找看是否可以删除
      getAddCount(basicId).then((response) => {
        this.editDiseaseList = response.data;
        if (this.editDiseaseList.length > 0) {
          // 说明要先删除掉疾病信息
          this.$message({
            message: "请先删除患者疾病信息",
            type: "warning",
            duration: 1500,
          });
          this.editDiseaseInfoList(basicId);
          this.isDelete = true;
        } else {
          // 说明可以直接删除
          this.$modal
            .confirm('是否确认删除【信息管理】编号为"' + basicId + '"的基础入组信息？')
            .then(() => {
              this.loading = true;
              return removeBasicInfo(basicId);
            })
            .then(() => {
              this.$modal.msgSuccess("删除成功");
            })
            .catch(() => {})
            .finally(() => {
              this.getNewPatient();
              this.getDataList();
              this.loading = false;
            });
        }
      });
    },
    /** 修改disease记录对话框关闭 */
    editDiseaseClose() {
      this.editDiseaseList = [];
      this.editDiseaseVisible = false;
      this.isDelete = false;
    },
    /** 初始化 */
    init() {
      this.getDataList();
      this.getDeptTree();
      this.getNewPatient();
    },
    /** 获取新注册患者集合 */
    getNewPatient() {
      getNewPatients().then((response) => {
        this.newPatients = response.data;
      });
    },
    /** 病情分类选择框改变 */
    handleSubChange(value) {
      this.queryParams.subId = value[value.length - 1];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getDataList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams.name = undefined;
      this.queryParams.patientNumber = undefined;
      this.queryParams.patientId = undefined;
      this.queryParams.subId = undefined;
      this.handleQuery();
    },
    /** 添加信息 */
    addInfo(id) {
      this.$router.push({
        name: "SaveAllInfo",
        params: {
          id: id, // 确保传递正确的参数值
        },
      });
    },
    /** 新增信息触发操作 */
    handleAddCommand(value) {
      this.$confirm("添加信息将绑定你为该患者的医生, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.addInfo(value);
      });
    },
    /** 更多操作触发 */
    handleMoreCommand(command, row) {
      switch (command) {
        case "handleInfoDetail":
          this.handleInfoDetail(row.id);
          break;
        case "handleAddDisease":
          this.handleAddDisease(row.id);
          break;
        default:
          break;
      }
    },
    /** 更多修改操作触发 */
    handleEditCommand(command, row) {
      let id;
      if (row === -1) {
        id = this.ids[0];
      } else {
        id = row.id;
      }
      switch (command) {
        case "editBasicInfo":
          this.editBasicInfo(id);
          break;
        case "editDiseaseInfoList":
          this.editDiseaseInfoList(id);
          break;
        default:
          break;
      }
    },
    /** 修改疾病信息 */
    editDiseaseInfo(value) {
      // 跳转到form页面
      console.log(this.$route.path);
      this.$router.push({
        name: "EditDiseaseInfo",
        params: {
          id: value.id,
        },
      });
    },
    /** 修改疾病信息列表展示 */
    editDiseaseInfoList(basicInfoId) {
      // 获取所有添加过的疾病信息
      this.editDiseaseLoading = true;
      this.editDiseaseVisible = true;
      getAddCount(basicInfoId).then((response) => {
        this.editDiseaseList = response.data;
        this.editDiseaseLoading = false;
      });
    },
    /** 修改基本入组信息 */
    editBasicInfo(basicInfoId) {
      // 跳转到form页面
      this.$router.push({
        name: "EditBasicInfo",
        params: {
          id: basicInfoId, // 确保传递正确的参数值
        },
      });
    },
    /** 获取数据列表 */
    getDataList() {
      this.loading = true;
      basicInfoList(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.dataList = response.data.list;
          this.total = response.data.totalCount;
          this.loading = false;
        }
      );
    },
    /** 添加患者病情分类 */
    addPatientSub() {
      this.$nextTick(() => {
        this.$refs.addPatientSub.addPatientSub();
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      subTree().then((response) => {
        this.deptOptions = response.data;
      });
    },
    /** 新增病情 */
    handleAddDisease(id) {
      // 带着basicId跳转到form页面
      this.$router.push({
        name: "SaveDiseaseInfo",
        params: { id: id },
      });
    },
    /** 获取婚姻类型 */
    getMarriage(marriage) {
      if (marriage == 0) {
        return "未婚";
      } else if (marriage == 1) {
        return "已婚";
      } else {
        return "离异";
      }
    },
    /** 打开抽屉的回调 */
    handleInfoDetail(id) {
      this.detailVisible = true;
      this.$nextTick(() => {
        this.$refs.infoDetail.init(id);
      });
    },
    /** 多选 */
    selectionChangeHandle(selection) {
      this.single = selection.length !== 1;
      this.ids = selection.map((item) => {
        return item.id;
      });
    },
    // 新增 / 修改
    addOrUpdateHandle(value) {
      if (value == 0) {
        this.$router.push({ name: "information-form-save" });
      } else {
        this.$router.push({
          name: "information-form-update",
          params: { id: value },
        });
      }
    },
  },
};
</script>
